package com.jiangxiaofei.citycloud.activity.freshfruits;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.GoodDetailBean;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.FreshConstant;
import com.jiangxiaofei.citycloud.mvp.FreshPersenter;
import com.jiangxiaofei.citycloud.util.PermissionUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * @author: JiangXFei
 * @date: 2017/11/21 0021
 * @content: 果蔬商品详情页
 */

public class FreshFruitsGoodDetailActivity extends BaseActivity
        implements FreshConstant.GetGoodDetailView {
    @InjectView(R.id.img_good_photo)
    ImageView imgGoodPhoto;
    @InjectView(R.id.tv_good_title)
    TextView tvGoodTitle;
    @InjectView(R.id.tv_good_price)
    TextView tvGoodPrice;
    @InjectView(R.id.tv_good_location)
    TextView tvGoodLocation;
    @InjectView(R.id.tv_good_time)
    TextView tvGoodTime;
    @InjectView(R.id.tv_good_look_count)
    TextView tvGoodLookCount;
    @InjectView(R.id.tv_good_content)
    TextView tvGoodContent;
    @InjectView(R.id.tv_qq)
    TextView tvQq;
    @InjectView(R.id.tv_call)
    TextView tvCall;
    @InjectView(R.id.recy_good_photos)
    RecyclerView recyGoodPhotos;
    private int goodId = -1;
    private String phoneNum;
    private FreshConstant.GetGoodDetailPersenter getGoodDetailPersenter;
    private ImgLoadeRecyAdapter imgLoadeRecyAdapter;
    private List<String> imgList = new ArrayList<>();

    @Override
    protected void initView(Bundle savedInstanceState) {
        mActivity = this;
        mContext = this;
        setContentView(R.layout.activity_fresh_fruits_good_detail_layout);
        ButterKnife.inject(this);
        initTitle();
        initImgAdapter();
        Intent intent = getIntent();
        goodId = intent.getIntExtra(StringConstants.FRESHGOODID, -1);
        getGoodDetailPersenter = new FreshPersenter(mContext, (FreshConstant.GetGoodDetailView) mActivity);
        getGoodDetailPersenter.getGoodDetail();
    }

    @Override
    protected void setListener() {
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        setLeftOneBg(R.drawable.ic_back_black);
        setCTitle("商品信息");
        mCenterTitle.setTextColor(Color.parseColor("#7b7b7b"));
        rl_layout.setBackgroundColor(Color.parseColor("#ffffff"));
    }

    @OnClick({R.id.tv_call})
    public void bOnclick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_call:
                initDetailPopup(v1 -> {
                    mPopupWindowPublish.dismiss();
                    PermissionUtil.CallPhone(mContext, phoneNum);
                }, v2 -> {
                    mPopupWindowPublish.dismiss();
                    PermissionUtil.sendMsg(mContext, phoneNum);
                }, v3 -> {
                    mPopupWindowPublish.dismiss();
                });
                showDetailPopup();
                break;
        }
    }

    /**
     * 初始化商品详情图片适配器
     */
    private void initImgAdapter() {
        imgLoadeRecyAdapter = new ImgLoadeRecyAdapter(R.layout.item_flea_good_image, imgList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyGoodPhotos.setLayoutManager(linearLayoutManager);
        recyGoodPhotos.setAdapter(imgLoadeRecyAdapter);
    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public int getGoodDetailId() {
        return goodId;
    }

    @Override
    public void getGoodDetailSuccess(GoodDetailBean bean) {
        phoneNum = bean.getPhonenum();
        tvGoodContent.setText(bean.getDescription());
        tvGoodPrice.setText(bean.getPrice());
        tvGoodLocation.setText(bean.getProarea());
        tvGoodTime.setText(bean.getIssue());
        tvGoodLookCount.setText(bean.getVisitors());
        tvGoodTitle.setText(bean.getTitle());
        String[] imgs = bean.getImg().split(",");
        Glide.with(mContext)
                .load(imgs[0])
                .apply(new RequestOptions()
                        .placeholder(R.drawable.ic_no_pic)
                        .error(R.drawable.ic_no_pic))
                .into(imgGoodPhoto);
        for (int i = 0; i < imgs.length; i++) {
            if (i > 0) {
                imgList.add(imgs[i]);
            }
        }
        imgLoadeRecyAdapter.notifyDataSetChanged();
    }

    private class ImgLoadeRecyAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public ImgLoadeRecyAdapter(int layoutResId, @Nullable List<String> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            ImageView imgView = helper.getView(R.id.img_view);
            Glide.with(mContext).load(item).into(imgView);
        }
    }
}
